//
// Created by dukang wang on 2024/12/19.
//

#include <jni.h>
#include <string>
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
#include "../AndroidLog.h"
#include "AudioRecorder.h"

using namespace hy;

extern "C"
JNIEXPORT jlong JNICALL
Java_com_hy_opensles_HYOpenSlRecorder_CreateInstance(JNIEnv *env, jobject thiz) {
    AudioRecorder *audioRecorder = AudioRecorder::CreateInstance();
    //转换为指针地址，返回给应用层，下面操作的时候传入
    long handler = reinterpret_cast<long>(audioRecorder);
    return handler;
}

extern "C"
JNIEXPORT jint JNICALL
Java_com_hy_opensles_HYOpenSlRecorder_Init(JNIEnv *env, jobject thiz, jlong handler) {
    AudioRecorder *audioRecorder = reinterpret_cast<AudioRecorder *>(handler);
    int result = audioRecorder->Init();
    return result;
}
extern "C"
JNIEXPORT void JNICALL
Java_com_hy_opensles_HYOpenSlRecorder_StartRecord(JNIEnv *env, jobject thiz, jlong handler,
                                                  jstring audio_path) {

    auto _audio_path = env->GetStringUTFChars(audio_path, nullptr);
    std::string audioPath = std::string(_audio_path);
    env->ReleaseStringUTFChars(audio_path, _audio_path);

    AudioRecorder *audioRecorder = reinterpret_cast<AudioRecorder *>(handler);
    audioRecorder->startRecord(audioPath);
}
extern "C"
JNIEXPORT void JNICALL
Java_com_hy_opensles_HYOpenSlRecorder_StopRecord(JNIEnv *env, jobject thiz, jlong handler) {

    AudioRecorder *audioRecorder = reinterpret_cast<AudioRecorder *>(handler);
    audioRecorder->stopRecord();
}
extern "C"
JNIEXPORT void JNICALL
Java_com_hy_opensles_HYOpenSlRecorder_Release(JNIEnv *env, jobject thiz, jlong handler) {
    AudioRecorder *audioRecorder = reinterpret_cast<AudioRecorder *>(handler);
    audioRecorder->release();
}